package com.group.itms.routes;

import java.sql.SQLException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.group.itms.routes.data.Advertisement;
import com.group.itms.routes.data.BObject;
import com.group.itms.routes.data.BObjectOption;
import com.group.itms.routes.data.BObjectTopic;
import com.group.itms.routes.data.ObjectGps;
import com.group.itms.routes.data.ObjectMap;
import com.group.itms.routes.data.ObjectType;
import com.group.itms.routes.data.Option;
import com.group.itms.routes.data.Topic;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
	private static final String DATABASE = "etrails.db";
	private static final int DATABASE_VERSION = 5;

	private Dao<BObject, Integer> objectDao;
	private Dao<BObjectOption, Integer> objectOptionDao;
	private Dao<BObjectTopic, Integer> objectTopicDao;
	private Dao<Option, Integer> optionDao;
	private Dao<Topic, Integer> topicDao;
	private Dao<ObjectType, Integer> typeDao;
	private Dao<ObjectGps, Integer> gpsDao;
	private Dao<ObjectMap, Integer> objMapDao;
	private Dao<Advertisement, Integer> advertDao;
	
	public DatabaseHelper(Context context) {
		super(context, DATABASE, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase database,
			ConnectionSource connectionSource) {
		try {
			TableUtils.createTable(connectionSource, BObject.class);
			TableUtils.createTable(connectionSource, BObjectOption.class);
			TableUtils.createTable(connectionSource, BObjectTopic.class);
			TableUtils.createTable(connectionSource, Option.class);
			TableUtils.createTable(connectionSource, Topic.class);
			TableUtils.createTable(connectionSource, ObjectType.class);
			TableUtils.createTable(connectionSource, ObjectGps.class);
			TableUtils.createTable(connectionSource, ObjectMap.class);
			TableUtils.createTable(connectionSource, Advertisement.class);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase database,
			ConnectionSource connectionSource, int oldVersion, int newVersion) {
		try {
			TableUtils.dropTable(connectionSource, BObject.class, true);
			TableUtils.dropTable(connectionSource, BObjectOption.class, true);
			TableUtils.dropTable(connectionSource, BObjectTopic.class, true);
			TableUtils.dropTable(connectionSource, Option.class, true);
			TableUtils.dropTable(connectionSource, Topic.class, true);
			TableUtils.dropTable(connectionSource, ObjectType.class, true);
			TableUtils.dropTable(connectionSource, ObjectGps.class, true);
			TableUtils.dropTable(connectionSource, ObjectMap.class, true);
			TableUtils.dropTable(connectionSource, Advertisement.class, true);
			onCreate(database);
			;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	public Dao<BObject, Integer> getObjectDao() {
		if (objectDao == null) {
			try {
				objectDao = getDao(BObject.class);
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
		return objectDao;
	}

	public Dao<BObjectOption, Integer> getObjectOptionDao() {
		if (objectOptionDao == null) {
			try {
				objectOptionDao = getDao(BObjectOption.class);
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
		return objectOptionDao;
	}

	public Dao<BObjectTopic, Integer> getObjectTopicDao() {
		if (objectTopicDao == null) {
			try {
				objectTopicDao = getDao(BObjectTopic.class);
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
		return objectTopicDao;
	}

	public Dao<Option, Integer> getOptionDao() {
		if (optionDao == null) {
			try {
				optionDao = getDao(Option.class);
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
		return optionDao;
	}

	public Dao<Topic, Integer> getTopicDao() {
		if (topicDao == null) {
			try {
				topicDao = getDao(Topic.class);
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
		return topicDao;
	}

	public Dao<ObjectType, Integer> getTypeDao() {
		if (typeDao == null) {
			try {
				typeDao = getDao(ObjectType.class);
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
		return typeDao;
	}

	public Dao<ObjectGps, Integer> getGpsDao() {
		if (gpsDao == null) {
			try {
				gpsDao = getDao(ObjectGps.class);
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
		return gpsDao;
	}
	
	public Dao<ObjectMap, Integer> getObjectMapDao() {
		if (objMapDao == null) { 
			try {
				objMapDao = getDao(ObjectMap.class);
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
		return objMapDao;
	}
	
	public Dao<Advertisement, Integer> getAdvertDao() {
		if (advertDao == null) {
			try {
				advertDao = getDao(Advertisement.class);
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
		return advertDao;
	}
	
}